﻿Imports System.Text
Imports System.Windows.Media.Animation
Imports System.Windows.Controls
Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Threading
Imports System.ComponentModel.Composition

Namespace VsSpeakerCompanion.UI
	<Export(GetType(IWidgetAnimationService))>
	Friend Class WidgetAnimationService
		Implements IWidgetAnimationService
        Private widgets As New List(Of IWidget)
        Private WithEvents timer As New DispatcherTimer

		Friend Sub New()
			' Set interval to 1.5 seconds
			timer.Interval = New TimeSpan(0, 0, 0, 0, 1500)
        End Sub

		''' <summary>
		''' See <see cref="IWidgetAnimationService"/> for more information
		''' </summary>
		''' <param name="widget"></param>
		Public Sub [Stop](ByVal widget As IWidget) Implements IWidgetAnimationService.Stop
			If widgets.Contains(widget) Then
				widgets.Remove(widget)
			End If
			If widgets.Count = 0 Then
				timer.Stop()
			End If
		End Sub

		''' <summary>
		''' See <see cref="IWidgetAnimationService"/> for more information
		''' </summary>
		''' <param name="widget"></param>
		Public Sub Start(ByVal widget As IWidget) Implements IWidgetAnimationService.Start
            If Not widgets.Contains(widget) Then
                widgets.Add(widget)
            End If
			timer.Start()
		End Sub

        Private Sub timer_Tick() Handles timer.Tick
            For Each widget As IWidget In widgets
                Dim control = TryCast(widget.VisualElement, FrameworkElement)
                control.BeginStoryboard(CreateStoryBoard(control))
            Next widget
        End Sub

		Private Function CreateStoryBoard(ByVal control As FrameworkElement) As Storyboard
			'<Storyboard x:Key="Flash">
			'    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)">
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.2500000" Value="0.850"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.375" Value="1"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.4750000" Value="0.950"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.7500000" Value="0.975"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
			'    </DoubleAnimationUsingKeyFrames>
			'    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.2500000" Value="0.850"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.375" Value="1"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.4750000" Value="0.950"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:00.7500000" Value="0.975"/>
			'        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
			'    </DoubleAnimationUsingKeyFrames>
			'</Storyboard>
			'<UserControl.RenderTransform>
			'    <ScaleTransform ScaleX="1" ScaleY="1" x:Name="ScaleTransform"/>
			'</UserControl.RenderTransform>

			Dim storyboard As Storyboard

            control.RenderTransform = New ScaleTransform(1, 1)
			control.RenderTransformOrigin = New Point(0.5, 0.5)

            storyboard = New Storyboard
            Dim scaleX As New DoubleAnimationUsingKeyFrames
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(0.850, KeyTime.FromTimeSpan(New TimeSpan(2500000))))
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(3750000))))
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(0.950, KeyTime.FromTimeSpan(New TimeSpan(4750000))))
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(6000000))))
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(0.975, KeyTime.FromTimeSpan(New TimeSpan(7500000))))
			scaleX.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(10000000))))
            Dim scaleY As New DoubleAnimationUsingKeyFrames
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(0.850, KeyTime.FromTimeSpan(New TimeSpan(2500000))))
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(3750000))))
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(0.950, KeyTime.FromTimeSpan(New TimeSpan(4750000))))
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(6000000))))
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(0.975, KeyTime.FromTimeSpan(New TimeSpan(7500000))))
			scaleY.KeyFrames.Add(New SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(New TimeSpan(10000000))))

			storyboard.Children.Add(scaleX)
			Storyboard.SetTargetProperty(scaleX, New System.Windows.PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"))

			storyboard.Children.Add(scaleY)
			Storyboard.SetTargetProperty(scaleY, New System.Windows.PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"))

			Return storyboard
		End Function
	End Class
End Namespace
